package com.ckd.flyingtrip.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.javabean.OrderBean;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.MyHolder> {

    private Context context;
    private List<OrderBean.DataBean.ListBean> list;
    public OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onClick(View view, int position);
    }

    public OrderAdapter(Context context, List<OrderBean.DataBean.ListBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyHolder(LayoutInflater.from(context).inflate(R.layout.item_order, null));
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        switch (list.get(position).getOrderType()) {
            case "1":
                holder.orderId.setText("押金订单：" + list.get(position).getOrderId());
                holder.ll1.setVisibility(View.VISIBLE);
                holder.ll2.setVisibility(View.GONE);
                holder.ll1_ll1.setVisibility(View.VISIBLE);
                holder.ll1_ll2.setVisibility(View.GONE);
                holder.ll1_ll3.setVisibility(View.GONE);
                holder.ll1_view1.setVisibility(View.GONE);
                holder.ll1_view2.setVisibility(View.GONE);
                break;
            case "2":
                holder.orderId.setText("租车订单：" + list.get(position).getOrderId());
                holder.ll1.setVisibility(View.VISIBLE);
                holder.ll2.setVisibility(View.GONE);
                holder.ll1_ll1.setVisibility(View.GONE);
                holder.ll1_ll2.setVisibility(View.VISIBLE);
                holder.ll1_ll3.setVisibility(View.VISIBLE);
                holder.ll1_view1.setVisibility(View.GONE);
                holder.ll1_view2.setVisibility(View.VISIBLE);
                break;
            case "3":
                holder.orderId.setText("买车订单：" + list.get(position).getOrderId());
                holder.ll1.setVisibility(View.GONE);
                holder.ll2.setVisibility(View.VISIBLE);
                break;
        }
        holder.name.setText(list.get(position).getBicycleName());
        Glide.with(context).load(list.get(position).getBicyclePictureUrl()).into(holder.icon);
        holder.ll2_textView1.setText(list.get(position).getOrderMoney());
        holder.ll1_textView1.setText(list.get(position).getOrderMoney());
        holder.ll1_textView2.setText(list.get(position).getOrderMoney());

        int Days = list.get(position).getOrderBuyDays().equals("") ? 0 : Integer.parseInt(list.get(position).getOrderBuyDays());
        String endTime = getOldDateByDay(parseServerTime(list.get(position).getOrderPayTime(), "yyyy-MM-dd HH:mm:ss"), Days, "yyyy.MM.dd");
        holder.ll1_textView3.setText(list.get(position).getOrderPayTime().substring(0, 4) + "." + list.get(position).getOrderPayTime().substring(5, 7) + "." +
                list.get(position).getOrderPayTime().substring(8, 10) + "-" + endTime);


        holder.ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClick(v, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        TextView orderId, name, ll1_textView1, ll1_textView2, ll1_textView3, ll2_textView1;
        ImageView icon;
        LinearLayout ll, ll1, ll2, ll1_ll1, ll1_ll2, ll1_ll3;
        View ll1_view1, ll1_view2;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            orderId = itemView.findViewById(R.id.item_order_orderId);
            name = itemView.findViewById(R.id.item_order_name);
            ll1_textView1 = itemView.findViewById(R.id.item_order_ll1_textView1);
            ll1_textView2 = itemView.findViewById(R.id.item_order_ll1_textView2);
            ll1_textView3 = itemView.findViewById(R.id.item_order_ll1_textView3);
            ll2_textView1 = itemView.findViewById(R.id.item_order_ll2_textView1);
            icon = itemView.findViewById(R.id.item_order_icon);
            ll = itemView.findViewById(R.id.item_order_ll);
            ll1 = itemView.findViewById(R.id.item_order_ll1);
            ll1_ll1 = itemView.findViewById(R.id.item_order_ll1_ll1);
            ll1_ll2 = itemView.findViewById(R.id.item_order_ll1_ll2);
            ll1_ll3 = itemView.findViewById(R.id.item_order_ll1_ll3);
            ll2 = itemView.findViewById(R.id.item_order_ll2);
            ll1_view1 = itemView.findViewById(R.id.item_order_ll1_view1);
            ll1_view2 = itemView.findViewById(R.id.item_order_ll1_view2);
        }
    }

    private Date parseServerTime(String serverTime, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINESE);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        Date date = null;
        try {
            date = sdf.parse(serverTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }


    private String getOldDateByDay(Date beginDate, int distanceDay, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd";
        }
        SimpleDateFormat dft = new SimpleDateFormat(format);
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay);
        Date endDate = null;
        try {
            endDate = dft.parse(dft.format(date.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dft.format(endDate);
    }

}
